//index.js
//获取应用实例
var app = getApp()
// var dialog = require("../../utils/dialog.js")
// var wxNotificationCenter = require("../../utils/WxNotificationCenter.js")

Page({

  data: {
    contentList:[],
    currentType:1, 
    types:[]
  },
  //加载第一个类型的列表
  onLoad:function(){
   

    this.getList(this.data.currentType)
    // 添加通知监听
    // wxNotificationCenter.addNotification("typesChangeNotification",this.typesChangeNotificationHandler,this)
  },
 
 
  getList:function(type){

        var that = this
        //请求数据
        wx.request({
          url:'https://xhapp.lhxq.top/index.php?s=/api/article/index&wxapp_id=10001',
          success:function(ret){
            let cates = ret.data.data.categoryList
            that.setData({
              types:cates
              })
        
              if(ret.data.data.categoryList.length) {
                that.setData({
                  currentType:cates[0].category_id
                  })
              }
              that.getListTopic()
          },
          complete:function(){
            wx.stopPullDownRefresh()
            setTimeout(function(){
             
            },1000)
          }
        })
  },

  getListTopic(){

    var that = this
    //请求数据
    wx.request({
      url:'https://xhapp.lhxq.top/index.php?s=/api/article/lists&wxapp_id=10001',

      data:{
        category_id: that.data.currentType
      },
      success:function(ret){
        let cates = ret.data.data.list.data
        that.setData({
          contentList:cates
          })

      },
      complete:function(){
        wx.stopPullDownRefresh()
        setTimeout(function(){
         
        },1000)
      }
    })
  },
  onPullDownRefresh:function(){
    this.getList(this.data.currentType)
  },
  //点击某一个title条
  changeType:function(e){
    var type = e.currentTarget.dataset.value

    console.log('type :' + type)
    if(type == this.data.currentType){
      return;
    }
    this.setData({currentType:type})
 
    this.getListTopic()
    // this.getList(type)
  },
  gotoTypeEdit:function(e){
    wx.navigateTo({
      url: '/pages/types/types?id=1',
    })
  },
  gotoAlbum:function(e){
    let param = e.currentTarget.dataset, title = param.title, id=param.id
    var url = "../album/album?title="+title+"&id="+id.replace(".","##");
    wx.navigateTo({url:url})
  },

  onShareAppMessage: function () {

    

  },

    /**
   * 跳转文章详情页
   */
  onTargetDetail(e) {
    wx.navigateTo({
      url: '/pages/article/article?article_id=' + e.currentTarget.dataset.id
    });
  },

  
})
